<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "config/config.php";
include "auth.php";
include "DBManager.php";

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------

if (!(isset($_POST['from']))) {
    exit("参数不完整");
}

$from = htmlspecialchars($_POST['from']);
$content = htmlspecialchars($_POST['content']);

// -----------------------------------------------------------------------------

try {
    switch ($from) {
        case "daily":
            $db->writeToDaily($content);
            break;
        case "weekly":
            if (!(isset($_POST['dayOfWeek']))) {
                exit("参数不完整。");
            }
            $dayOfWeek = htmlspecialchars($_POST['dayOfWeek']);
            $db->writeToWeekly($content, $dayOfWeek);
            break;
        case "monthly":
            if (!(isset($_POST['dayOfMonth']))) {
                exit("参数不完整。");
            }
            $dayOfMonth = htmlspecialchars($_POST['dayOfMonth']);
            $db->writeToMonthly($content, $dayOfMonth);
            break;
        case "monthlyLunar":
            if (!(isset($_POST['dayOfMonth']))) {
                exit("参数不完整。");
            }
            $dayOfMonth = htmlspecialchars($_POST['dayOfMonth']);
            $db->writeToMonthlyLunar($content, $dayOfMonth);
            break;
        case "yearly":
            if (!(isset($_POST['monthAndDay']))) {
                exit("参数不完整。");
            }
            $monthAndDay= htmlspecialchars($_POST['monthAndDay']);
            $db->writeToYearly($content, $monthAndDay);
            break;
        case "yearlyLunar":
            if (!(isset($_POST['monthAndDay']))) {
                exit("参数不完整。");
            }
            $monthAndDay= htmlspecialchars($_POST['monthAndDay']);
            $db->writeToYearlyLunar($content, $monthAndDay);
            break;
        case "specificDate":
            if (!(isset($_POST['date']))) {
                exit("参数不完整。");
            }
            $date = htmlspecialchars($_POST['date']);
            $db->writeToSpecificDate($content, $date);
    }

    echo "保存成功！";
} catch (Exception $e) {
    echo $e->getMessage();
}
